#include "Qt3DPicker.h"

#include "Offset2DHandler.h"
#include "CmdManager.h"
#include "Registry.h"
#include "ActorTable.h"
#include "Scene.h"
#include "SendPointHandlerBase.h"
#include "SelectHandler3D.h"
#include "OSGNodeMask.h"
#include "Operation/OpAdd.h"
#include "Ent.h"
#include "EntFromEdge.h"
#include "Spline2D.h"

Offset2DHandler::Offset2DHandler()
{
}

Offset2DHandler::~Offset2DHandler()
{
}

void Offset2DHandler::input(const QVariant& data, const QString& rawInput, CommandSource cs)
{
	Registry &reg = Registry::instance();
	Scene* scene = reg.get<Scene>(constructSceneID);
	auto sph = getSendPointHandler();
	auto selector = qobject_cast<SelectHandler3D*>(getSelectHandler());
	if (data.type() == QVariant::Bool)
	{
		bool esc = !data.toBool();
		if (esc)
		{
			selector->clearSelection();
			clearInput();
			emit finished();
			return;
		}
	}

	if (status() == 0)
	{
		if (selector)
		{
			selector->clearSelection();
			selector->beginSelect(D2CurveMask);
			prompt(1, tr("选择对象: "));
		}
		else
		{
			emit finished();
		}
		return;
	}

	if (status() == 1)
	{
		if (selector && selector->done())
		{
			auto selections = selector->selectionSet();
			if (selections.size() > 0)
			{
				auto selection = *selections.begin();
				std::shared_ptr<Ent> ent = std::dynamic_pointer_cast<Ent>(selection);
				auto parent = ent->getParent();
				UCS* ucs = dynamic_cast<UCS*>(parent);
				if (ucs)
				{
					m_ucs = ucs;
				}
				else
				{
					remark(tr("请选择二维图形"));
					emit finished();
					return;
				}

				for (auto selection : selections)
				{
					std::shared_ptr<Ent2D> ent2d = std::dynamic_pointer_cast<Ent2D>(selection);
					auto parent = ent2d->getParent();
					UCS* ucs = dynamic_cast<UCS*>(parent);
					if (ucs != m_ucs)
						continue;
					m_origins.insert(ent2d);
				}

				if (sph)
				{
					sph->setPointMask(CS_LBTNDOWN | CS_MOUSEMOVE);
				}
				enableMouseHandler(MouseHandlerID::PointSender);
				cmdManager()->setPlane((Plane2DKind)m_ucs->getKind());
				prompt(2, tr("指定偏移距离:"));
			}
			else
			{
				emit finished();
			}
		}
		return;
	}

	if(status() == 2)
	{
		osg::Vec3d ucspt;
		if (getPoint2D(*m_ucs, data, rawInput, cs, ucspt))
		{
			if (cs == CS_MOUSEMOVE)
				return;
			m_start = m_end = ucspt;

			auto matrix = m_ucs->getMatrix();
			show3DRubberLine(true);
			set3DRubberLineP1(m_start * matrix);
			set3DRubberLineP2(m_end * matrix);
			prompt(3, tr("指定偏移距离<指定第二点>:"));
		}
		else if (data.type() == QVariant::Double)
		{
			m_offset = data.value<double>();
			prompt(4, tr("指定要偏移的那一侧上的点:"));
			return;
		}
		return;
	}

	if (status() == 3)
	{
		osg::Vec3d ucspt;
		if (getPoint2D(*m_ucs, data, rawInput, cs, ucspt))
		{
			if (cs == CS_MOUSEMOVE)
			{
				m_end = ucspt;
				auto matrix = m_ucs->getMatrix();
				set3DRubberLineP2(m_end * matrix);
				return;
			}		
			else
			{
				const osg::Vec3d offset = m_end - m_start;
				m_offset = offset.length();
				show3DRubberLine(false);
			}
			prompt(4, tr("指定要偏移的那一侧上的点:"));
		}
		else if (data.type() == QVariant::Double)
		{
			m_offset = data.value<double>();
			prompt(4, tr("指定要偏移的那一侧上的点:"));
		}
		return;
	}

	if (status() == 4)
	{
		osg::Vec3d ucspt;
		if (getPoint2D(*m_ucs, data, rawInput, cs, ucspt))
		{
			if (cs == CS_MOUSEMOVE)
				return;
			selector->clearSelection();
			std::set<std::shared_ptr<INode>> children;
			for (auto origin : m_origins)
			{
				auto cloneEnt = origin->clone();
				std::shared_ptr<Ent2DCurve> clone = std::dynamic_pointer_cast<Ent2DCurve>(cloneEnt);				
				bool success = clone->offset(m_offset, ucspt);
				if (!success)
				{
					remark(tr("偏移失败"));
					emit finished();
					return;
				}					
				children.insert(clone);
				
			}
			auto undoStack = scene->undoStack();
			undoStack->push(new OpAdd(children));
			emit finished();
			return;
		}
	}
}